//
//  GLCell.swift
//  AICleaner
//
//  Created by firefly on 2025/1/27.
//

import SwiftUI

struct GLCell<Title: View, Subtitle: View, Leading: View, Trailing: View>: View {
    var title: Title
    var subtitle: Subtitle
    var leading: Leading
    var trailing: Trailing
    var onPressed: (() -> Void)?

    init(
        @ViewBuilder title: () -> Title,
        @ViewBuilder subtitle: () -> Subtitle,
        @ViewBuilder leading: () -> Leading,
        @ViewBuilder trailing: () -> Trailing,
        onPressed: (() -> Void)? = nil
    ) {
        self.title = title()
        self.subtitle = subtitle()
        self.leading = leading()
        self.trailing = trailing()
        self.onPressed = onPressed
    }

    var body: some View {
        HStack(spacing: 10) {
            if !(leading is EmptyView) {
                leading
                Spacer(minLength: 16)
            }

            if !(subtitle is EmptyView) {
                VStack(alignment: .leading, spacing: 4) {
                    title
                    subtitle
                }
            } else {
                title
                    .frame(maxWidth: .infinity, alignment: .leading)
            }

            if !(trailing is EmptyView) {
                Spacer(minLength: 16)
                trailing
            }
        }
        .contentShape(Rectangle())
        .onTapGesture {
            onPressed?()
        }
    }
}

// MARK: - 扩展：便捷初始化方法
extension GLCell where Subtitle == EmptyView, Leading == EmptyView, Trailing == EmptyView {
    init(
        @ViewBuilder title: () -> Title,
        onPressed: (() -> Void)? = nil
    ) {
        self.init(
            title: title,
            subtitle: { EmptyView() },
            leading: { EmptyView() },
            trailing: { EmptyView() },
            onPressed: onPressed
        )
    }
}

extension GLCell where Leading == EmptyView, Trailing == EmptyView {
    init(
        @ViewBuilder title: () -> Title,
        @ViewBuilder subtitle: () -> Subtitle,
        onPressed: (() -> Void)? = nil
    ) {
        self.init(
            title: title,
            subtitle: subtitle,
            leading: { EmptyView() },
            trailing: { EmptyView() },
            onPressed: onPressed
        )
    }
}

extension GLCell where Subtitle == EmptyView {
    init(
        @ViewBuilder title: () -> Title,
        @ViewBuilder leading: () -> Leading,
        @ViewBuilder trailing: () -> Trailing,
        onPressed: (() -> Void)? = nil
    ) {
        self.init(
            title: title,
            subtitle: { EmptyView() },
            leading: leading,
            trailing: trailing,
            onPressed: onPressed
        )
    }
}

extension GLCell where Leading == EmptyView {
    init(
        @ViewBuilder title: () -> Title,
        @ViewBuilder subtitle: () -> Subtitle,
        @ViewBuilder trailing: () -> Trailing,
        onPressed: (() -> Void)? = nil
    ) {
        self.init(
            title: title,
            subtitle: subtitle,
            leading: { EmptyView() },
            trailing: trailing,
            onPressed: onPressed
        )
    }
}

extension GLCell where Trailing == EmptyView {
    init(
        @ViewBuilder title: () -> Title,
        @ViewBuilder subtitle: () -> Subtitle,
        @ViewBuilder leading: () -> Leading,
        onPressed: (() -> Void)? = nil
    ) {
        self.init(
            title: title,
            subtitle: subtitle,
            leading: leading,
            trailing: { EmptyView() },
            onPressed: onPressed
        )
    }
}

// 第一版本
//struct GLCell<
//    TitleView: View,
//    SubTitleView: View,
//    LeadingView: View,
//    TrailingView: View
//>: View {
//    // MARK: - 核心参数
//    private let title: TitleView
//    private let subtitle: SubTitleView
//    private let leading: LeadingView
//    private let trailing: TrailingView
//    private let onPressed: (() -> Void)?
//    
//    // MARK: - 初始化方法
//    init(
//        @ViewBuilder title: () -> TitleView,
//        @ViewBuilder subtitle: () -> SubTitleView,
//        @ViewBuilder leading: () -> LeadingView,
//        @ViewBuilder trailing: () -> TrailingView,
//        onPressed: (() -> Void)? = nil
//    ) {
//        self.title = title()
//        self.subtitle = subtitle()
//        self.leading = leading()
//        self.trailing = trailing()
//        self.onPressed = onPressed
//    }
//    
//    var body: some View {
//        HStack(spacing: 10) {
//            // 左侧内容
//            leadingView
//            
//            // 主内容区域
//            mainContent
//            
//            // 右侧内容
//            trailingView
//        }
//        .contentShape(Rectangle())
//        .onTapGesture { onPressed?() }
//    }
//}
//
//// MARK: - 扩展：便捷初始化方法
//extension GLCell where SubTitleView == EmptyView, LeadingView == EmptyView, TrailingView == EmptyView {
//    init(
//        @ViewBuilder title: () -> TitleView,
//        onPressed: (() -> Void)? = nil
//    ) {
//        self.init(
//            title: title,
//            subtitle: { EmptyView() },
//            leading: { EmptyView() },
//            trailing: { EmptyView() },
//            onPressed: onPressed
//        )
//    }
//}
//
//extension GLCell where LeadingView == EmptyView, TrailingView == EmptyView {
//    init(
//        @ViewBuilder title: () -> TitleView,
//        @ViewBuilder subtitle: () -> SubTitleView,
//        onPressed: (() -> Void)? = nil
//    ) {
//        self.init(
//            title: title,
//            subtitle: subtitle,
//            leading: { EmptyView() },
//            trailing: { EmptyView() },
//            onPressed: onPressed
//        )
//    }
//}
//
//// MARK: - 内部布局组件
//private extension GLCell {
//    @ViewBuilder
//    var leadingView: some View {
//        if !isLeadingEmpty {
//            leading
//            Spacer(minLength: 16)
//        }
//    }
//    
//    @ViewBuilder
//    var trailingView: some View {
//        if !isTrailingEmpty {
//            Spacer(minLength: 16)
//            trailing
//        }
//    }
//    
//    @ViewBuilder
//    var mainContent: some View {
//        if !isSubtitleEmpty {
//            VStack(alignment: .leading, spacing: 4) {
//                title
//                subtitle
//            }
//        } else {
//            title
//                .frame(maxWidth: .infinity, alignment: .leading)
//        }
//    }
//    
//    var isLeadingEmpty: Bool {
//        String(describing: leading) == String(describing: EmptyView())
//    }
//    
//    var isTrailingEmpty: Bool {
//        String(describing: trailing) == String(describing: EmptyView())
//    }
//    
//    var isSubtitleEmpty: Bool {
//        String(describing: subtitle) == String(describing: EmptyView())
//    }
//}
